﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MagicBag.CRM.Model;
using MagicBag.CRM.BLL;
using System.Web;

namespace MagicBag.CRM.Service
{
    public class RoleMenuService : BaseService
    {
        public void Save(HttpContext context)
        {

            //角色ID
            int roleID;
            Int32.TryParse(context.Request["roleID"], out roleID);
            //菜单ID,这里用户在界面上选中的所有菜单的叶子节点
            string menuIDs = context.Request["menuIDs"];
            int[] arr = JsonHelper.ParseFromJson<List<int>>(menuIDs).ToArray();

            Result result = new Result();
            result.Success = true;

            try
            {
                RoleMenuBLL roleMenuBLL = new RoleMenuBLL();
                //获取当前角色的所有菜单
                IList<RoleMenu> roleMenus = roleMenuBLL.GetRoleMenusByRoleID(roleID);

                //1.删除取消选择的原菜单
                IList<RoleMenu> rmForDelete = roleMenus.Where(rm => !arr.Contains(rm.MenuID)).ToList();

                foreach (RoleMenu rm in rmForDelete)
                {
                    roleMenuBLL.Delete(rm.RoleID, rm.MenuID);//从数据库中删除
                    roleMenus.Remove(rm);//从内存中删除
                }
                //剩余的保留菜单
                int[] arr_left = roleMenus.Select(rm => rm.MenuID).ToArray();

                /*注：在1，2之间由于没有使用事务，将可能导致数据不一致的bug，
                 * 但概率极低，不会有严重后果，重新分配一次即可,后期优化可改变存储过程事务
                 */

                //2.插入新菜单中的非保留菜单（不在数据库中的菜单）
                int[] arr_new = arr.Where(a => !arr_left.Contains(a)).ToArray();

                //用户ID
                string userID = GetUserID();

                foreach (int id in arr_new)
                {
                    RoleMenu rm = new RoleMenu(roleID, id);
                    rm.CreateDate = DateTime.Now;
                    rm.CreateUserID = userID;
                    rm.LastUpdateDate = DateTime.Now;
                    rm.LastUpdateUserID = userID;
                    roleMenuBLL.Insert(rm);
                }

                //3.更新保留菜单（主要是更新【最后更新用户ID】【最后更新时间】
                foreach (RoleMenu rm in roleMenus)
                {
                    rm.LastUpdateDate = DateTime.Now;
                    rm.LastUpdateUserID = userID;
                    roleMenuBLL.Update(rm);
                }
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }

            ResponseJson(context, result.ToString());
        }

        public void GetRoleMenuIDsByRoleID(HttpContext context)
        {
            int roleID;
            Int32.TryParse(context.Request["roleID"], out roleID);
            RoleMenuBLL roleMenuBLL = new RoleMenuBLL();
            IList<RoleMenu> list = roleMenuBLL.GetRoleMenusByRoleID(roleID);

            int[] ids = list.Select(rm => rm.MenuID).ToArray();
            string s = string.Empty;

            if (ids.Length > 0)
                s = JsonHelper.GetJson<int[]>(ids);
            ResponseJson(context, s);
        }
    }
}
